﻿using ComponentFactory.Krypton.Toolkit;
using SRB.Tg.Model;
using SRB.Tg.Operate;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Teigha.DatabaseServices;

namespace SRB.Tg.Cad
{
    public partial class PileManagerForm : KryptonForm
    {
        DataRow _row;
        double _scale=50;
        public DataRow myrow;
        public bool isConfirm=false ;
        private static PileManagerForm _instance;
        public static PileManagerForm _instanceAdd;
        public static PileManagerForm getInstance(DataRow row, double scale,bool isModify=false)
        {
            if (_instance == null)
            {
                _instance = new PileManagerForm(row, scale);
            }
            else
            {
                if (!isModify)
                {
                    LoadDate(_instance, row, scale);
                }
                _instance.Refresh();
            }
            return _instance;
        }
        public static PileManagerForm getInstance(bool isNew=false)
        {
            if (_instanceAdd == null)
            {
                _instanceAdd = new PileManagerForm();
            }
            else
            {
                if(isNew)
                    _instanceAdd = new PileManagerForm();
            }
            return _instanceAdd;
        }
        public PileManagerForm()
        {
            InitializeComponent();
            InitDic();
            this.Refresh();
        }

        public void InitDic()
        {
            string sql = "select dict_label from system_dict_data where dict_type='reinforcement_grade'";

            System.Data.DataTable dt = MySqlHelper.ExecuteDataTable(sql, CommandType.Text);
            if(dt.Rows.Count>0)
            {
                reinforcement_grade.DataSource = dt;
                reinforcement_grade.DisplayMember = "dict_label";
                reinforcement_grade.ValueMember = "dict_label";
                reinforcement_grade.Text = "B500B";
            }
        }
        public PileManagerForm(DataRow row,double scale)
        {
            InitializeComponent();
            _row = row;
            _scale = scale;
            InitDic();
            this.configure_name.Text = row["configure_name"].ToString();
            this.pile_link_type.Text = row["pile_link_type"].ToString();
            this.bottom_encryption_length.Text = row["bottom_encryption_length"].ToString();
            this.bottom_encryption_spacing.Text = row["bottom_encryption_spacing"].ToString();

            this.concrete_protect_layer.Text = row["concrete_protect_layer"].ToString();

            this.main_reinforcement_diameter.Text = row["main_reinforcement_diameter"].ToString();
            this.main_reinforcement_number.Text = row["main_reinforcement_number"].ToString();

            this.outer_strengthen_reinforcement_diameter.Text = row["outer_strengthen_reinforcement_diameter"].ToString();
            this.outer_strengthen_reinforcement_spacing.Text = row["outer_strengthen_reinforcement_spacing"].ToString();
            this.pile_diameter.Text = row["pile_diameter"].ToString();
            this.pile_length.Text = row["pile_length"].ToString();
            this.pile_penetration_depth.Text = row["pile_penetration_depth"].ToString();
            this.reinforcement_grade.Text = row["reinforcement_grade"].ToString();
            this.spiral_reinforcement_diameter.Text = row["spiral_reinforcement_diameter"].ToString();
            this.top_encryption_length.Text = row["top_encryption_length"].ToString();
            this.top_encryption_spacing.Text = row["top_encryption_spacing"].ToString();
            this.middle_encryption_length.Text = row["middle_encryption_length"].ToString();
            this.middle_encryption_spacing.Text = row["middle_encryption_spacing"].ToString();
            this.top_pile_reinforcement_length.Text= row["top_pile_reinforcement_length"].ToString();
            this.pier_diameter.Text = row["pier_diameter"].ToString();
            this.pier_protect_layer.Text = row["pier_protect_layer"].ToString();
            this.pier_main_diameter.Text = row["pier_main_diameter"].ToString();
            this.pier_out_reinforcement_diameter.Text = row["pier_out_reinforcement_diameter"].ToString();
            this.pier_arc_length.Text = row["pier_arc_length"].ToString();
        }

        public static void LoadDate(PileManagerForm _instance,DataRow row, double scale)
        {
            _instance._row = row;
            _instance._scale = scale;
            _instance.configure_name.Text = row["configure_name"].ToString();
            _instance.pile_link_type.Text = row["pile_link_type"].ToString();
            _instance.bottom_encryption_length.Text = row["bottom_encryption_length"].ToString();
            _instance.bottom_encryption_spacing.Text = row["bottom_encryption_spacing"].ToString();

            _instance.concrete_protect_layer.Text = row["concrete_protect_layer"].ToString();

            _instance.main_reinforcement_diameter.Text = row["main_reinforcement_diameter"].ToString();
            _instance.main_reinforcement_number.Text = row["main_reinforcement_number"].ToString();

            _instance.outer_strengthen_reinforcement_diameter.Text = row["outer_strengthen_reinforcement_diameter"].ToString();
            _instance.outer_strengthen_reinforcement_spacing.Text = row["outer_strengthen_reinforcement_spacing"].ToString();
            _instance.pile_diameter.Text = row["pile_diameter"].ToString();
            _instance.pile_length.Text = row["pile_length"].ToString();
            _instance.pile_penetration_depth.Text = row["pile_penetration_depth"].ToString();
            _instance.reinforcement_grade.Text = row["reinforcement_grade"].ToString();
            _instance.spiral_reinforcement_diameter.Text = row["spiral_reinforcement_diameter"].ToString();

            _instance.top_encryption_length.Text = row["top_encryption_length"].ToString();
            _instance.top_encryption_spacing.Text = row["top_encryption_spacing"].ToString();
            _instance.middle_encryption_length.Text = row["middle_encryption_length"].ToString();
            _instance.middle_encryption_spacing.Text = row["middle_encryption_spacing"].ToString();
            _instance.top_pile_reinforcement_length.Text = row["top_pile_reinforcement_length"].ToString();
            _instance.pier_diameter.Text = row["pier_diameter"].ToString();
            _instance.pier_protect_layer.Text = row["pier_protect_layer"].ToString();
            _instance.pier_main_diameter.Text = row["pier_main_diameter"].ToString();
            _instance.pier_out_reinforcement_diameter.Text = row["pier_out_reinforcement_diameter"].ToString();
            _instance.pier_arc_length.Text = row["pier_arc_length"].ToString();
        }
        public void SaveAdd()
        {
            if (string.IsNullOrWhiteSpace(this.configure_name.Text))
            {
                MessageBox.Show("请填写桩基名称！", "提示");
                return;
            }
            else
            {
                string sqlS = "select * from t_pile_info_reinforcement where configure_name='"+ this.configure_name.Text + "'";

                System.Data.DataTable dt = MySqlHelper.ExecuteDataTable(sqlS, CommandType.Text);
                if(dt.Rows.Count>0)
                {
                    MessageBox.Show("桩基名称"+ this.configure_name.Text + "已存在，请修改桩基名称！", "提示");
                    return;
                }
            }
            if (string.IsNullOrWhiteSpace(this.pile_length.Text))
            {
                MessageBox.Show("请填写桩长！", "提示");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.pile_diameter.Text))
            {
                MessageBox.Show("请填写桩径！", "提示");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.concrete_protect_layer.Text))
            {
                MessageBox.Show("请填写保护层！", "提示");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.pile_penetration_depth.Text))
            {
                MessageBox.Show("请填写桩基伸入深度！", "提示");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.top_pile_reinforcement_length.Text))
            {
                MessageBox.Show("请填写桩顶部加密区长度！", "提示");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.main_reinforcement_diameter.Text))
            {
                MessageBox.Show("请填写主筋直径！", "提示");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.main_reinforcement_number.Text))
            {
                MessageBox.Show("请填写主筋根数！", "提示");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.top_encryption_length.Text))
            {
                MessageBox.Show("请填写顶部加密长度！", "提示");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.top_encryption_spacing.Text))
            {
                MessageBox.Show("请填写顶部加密钢筋间距！", "提示");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.middle_encryption_length.Text))
            {
                MessageBox.Show("请填写中部加密长度！", "提示");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.middle_encryption_spacing.Text))
            {
                MessageBox.Show("请填写中部加密钢筋间距！", "提示");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.bottom_encryption_length.Text))
            {
                MessageBox.Show("请填写底部加密长度！", "提示");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.bottom_encryption_spacing.Text))
            {
                MessageBox.Show("请填写底部加密钢筋间距！", "提示");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.outer_strengthen_reinforcement_diameter.Text))
            {
                MessageBox.Show("请填写圆形加强钢筋直径！", "提示");
                return;
            }
            if (string.IsNullOrWhiteSpace(this.outer_strengthen_reinforcement_spacing.Text))
            {
                MessageBox.Show("请填写圆形加强钢筋间距！", "提示");
                return;
            }
            string sql;
            if (pile_link_type.SelectedIndex == 0)
            {
                sql = string.Format("insert into t_pile_info_reinforcement(bottom_encryption_length,bottom_encryption_spacing,concrete_protect_layer,"
                     + "configure_name,main_reinforcement_diameter,main_reinforcement_number,middle_encryption_length,middle_encryption_spacing,"
                     + "outer_strengthen_reinforcement_diameter,outer_strengthen_reinforcement_spacing,"
                     + "pile_diameter,pile_length,pile_penetration_depth,reinforcement_grade,top_encryption_length,top_encryption_spacing,create_time,top_pile_reinforcement_length,pile_link_type,spiral_reinforcement_diameter) "
                     + "values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}')",
                       this.bottom_encryption_length.Text, this.bottom_encryption_spacing.Text,
                       this.concrete_protect_layer.Text, this.configure_name.Text,
                       this.main_reinforcement_diameter.Text, this.main_reinforcement_number.Text,
                       this.middle_encryption_length.Text, this.middle_encryption_spacing.Text,
                       this.outer_strengthen_reinforcement_diameter.Text,
                       this.outer_strengthen_reinforcement_spacing.Text, this.pile_diameter.Text, this.pile_length.Text,
                       this.pile_penetration_depth.Text, this.reinforcement_grade.Text, this.top_encryption_length.Text,
                       this.top_encryption_spacing.Text, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), this.top_pile_reinforcement_length.Text,this.pile_link_type.Text,this.spiral_reinforcement_diameter.Text);
            }
            else
            {
                if (string.IsNullOrWhiteSpace(this.pier_diameter.Text))
                {
                    MessageBox.Show("请填写墩身直径！", "提示");
                    return;
                }
                if (string.IsNullOrWhiteSpace(this.pier_protect_layer.Text))
                {
                    MessageBox.Show("请填写墩身保护层！", "提示");
                    return;
                }
                if (string.IsNullOrWhiteSpace(this.pier_main_diameter.Text))
                {
                    MessageBox.Show("请填写墩身主筋直径！", "提示");
                    return;
                }
                if (string.IsNullOrWhiteSpace(this.pier_out_reinforcement_diameter.Text))
                {
                    MessageBox.Show("请填写墩身箍筋直径！", "提示");
                    return;
                }
                if (string.IsNullOrWhiteSpace(this.spiral_reinforcement_diameter.Text))
                {
                    MessageBox.Show("请填写螺旋筋直径！", "提示");
                    return;
                }
                if (string.IsNullOrWhiteSpace(this.pier_arc_length.Text))
                {
                    MessageBox.Show("请填写墩身搭接弯曲长度！", "提示");
                    return;
                }
                sql = string.Format("insert into t_pile_info_reinforcement(bottom_encryption_length,bottom_encryption_spacing,concrete_protect_layer,"
                    + "configure_name,main_reinforcement_diameter,main_reinforcement_number,middle_encryption_length,middle_encryption_spacing,"
                    + "outer_strengthen_reinforcement_diameter,outer_strengthen_reinforcement_spacing,"
                    + "pile_diameter,pile_length,pile_penetration_depth,reinforcement_grade,top_encryption_length,top_encryption_spacing,create_time,"
                    + "top_pile_reinforcement_length, pier_diameter,pier_main_diameter,pier_out_reinforcement_diameter,pier_protect_layer,pile_link_type，spiral_reinforcement_diameter,pier_encryption_length,pier_encryption_spacing,pier_arc_length) "
                    + "values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}','{15}','{16}','{17}','{18}','{19}','{20}','{21}','{22}','{23}','{24}','{25}','{26}')",
                      this.bottom_encryption_length.Text, this.bottom_encryption_spacing.Text,
                      this.concrete_protect_layer.Text, this.configure_name.Text,
                      this.main_reinforcement_diameter.Text, this.main_reinforcement_number.Text,
                      this.middle_encryption_length.Text, this.middle_encryption_spacing.Text,
                      this.outer_strengthen_reinforcement_diameter.Text,
                      this.outer_strengthen_reinforcement_spacing.Text, this.pile_diameter.Text, this.pile_length.Text,
                      this.pile_penetration_depth.Text, this.reinforcement_grade.Text, this.top_encryption_length.Text,
                      this.top_encryption_spacing.Text, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"), this.top_pile_reinforcement_length.Text,
                      this.pier_diameter.Text,this.pier_main_diameter.Text,this.pier_out_reinforcement_diameter.Text,this.pier_protect_layer.Text,
                      this.pile_link_type.Text,this.spiral_reinforcement_diameter.Text, this.pier_encryption_length.Text, this.pier_encryption_spacing.Text,this.pier_arc_length.Text);
            }
            if (MySqlHelper.ExecuteNonQuery(sql, CommandType.Text) > 0)
            {
                string sqlM = "select * from t_pile_info_reinforcement where configure_name='" + this.configure_name.Text + "'";
                System.Data.DataTable dtM = MySqlHelper.ExecuteDataTable(sqlM, CommandType.Text);
                if (dtM.Rows.Count > 0)
                {
                    myrow = dtM.Rows[0];
                }
                isConfirm = true;
            }
        }
        public void SavModify()
        {
            if (_row != null)
            {
                if (!_row.IsNull("id"))
                {
                    string updatesql;
                    if (pile_link_type.SelectedIndex == 0)
                    {
                        updatesql = string.Format("update t_pile_info_reinforcement set bottom_encryption_length='{1}',bottom_encryption_spacing='{2}',"
                        + "concrete_protect_layer='{3}',configure_name='{4}',main_reinforcement_diameter='{5}',main_reinforcement_number='{6}',"
                           + "middle_encryption_length='{7}',middle_encryption_spacing='{8}',"
                        + "outer_strengthen_reinforcement_diameter='{9}',outer_strengthen_reinforcement_spacing='{10}',pile_diameter='{11}',pile_length='{12}',"
                        + "pile_penetration_depth='{13}',reinforcement_grade='{14}',top_encryption_length='{15}',top_encryption_spacing='{16}'"
                        + ",top_pile_reinforcement_length='{17}',pile_link_type='{18}',spiral_reinforcement_diameter='{19}'"
                        + " where id='{0}'", _row["id"].ToString(),
                       this.bottom_encryption_length.Text, this.bottom_encryption_spacing.Text,
                       this.concrete_protect_layer.Text, this.configure_name.Text,
                       this.main_reinforcement_diameter.Text, this.main_reinforcement_number.Text,
                       this.middle_encryption_length.Text, this.middle_encryption_spacing.Text,
                       this.outer_strengthen_reinforcement_diameter.Text,
                       this.outer_strengthen_reinforcement_spacing.Text, this.pile_diameter.Text, this.pile_length.Text,
                       this.pile_penetration_depth.Text, this.reinforcement_grade.Text, this.top_encryption_length.Text
                       , this.top_encryption_spacing.Text, this.top_pile_reinforcement_length.Text,this.pile_link_type.Text,this.spiral_reinforcement_diameter.Text);
                    }
                    else
                    {
                        updatesql = string.Format("update t_pile_info_reinforcement set bottom_encryption_length='{1}',bottom_encryption_spacing='{2}',"
                                                + "concrete_protect_layer='{3}',configure_name='{4}',main_reinforcement_diameter='{5}',main_reinforcement_number='{6}',"
                                                   + "middle_encryption_length='{7}',middle_encryption_spacing='{8}',"
                                                + "outer_strengthen_reinforcement_diameter='{9}',outer_strengthen_reinforcement_spacing='{10}',pile_diameter='{11}',pile_length='{12}',"
                                                + "pile_penetration_depth='{13}',reinforcement_grade='{14}',top_encryption_length='{15}',top_encryption_spacing='{16}'"
                                                + ",top_pile_reinforcement_length='{17}',pier_diameter='{18}',pier_main_diameter='{19}',pier_out_reinforcement_diameter='{20}',"
                                                + "pier_protect_layer='{21}',pile_link_type='{22}',spiral_reinforcement_diameter='{23}',pier_encryption_length='{24}',pier_encryption_spacing='{25}',pier_arc_length='{26}'"
                                                + " where id='{0}'", _row["id"].ToString(),
                                               this.bottom_encryption_length.Text, this.bottom_encryption_spacing.Text,
                                               this.concrete_protect_layer.Text, this.configure_name.Text,
                                               this.main_reinforcement_diameter.Text, this.main_reinforcement_number.Text,
                                               this.middle_encryption_length.Text, this.middle_encryption_spacing.Text,
                                               this.outer_strengthen_reinforcement_diameter.Text,
                                               this.outer_strengthen_reinforcement_spacing.Text, this.pile_diameter.Text, this.pile_length.Text,
                                               this.pile_penetration_depth.Text, this.reinforcement_grade.Text, this.top_encryption_length.Text,
                                               this.top_encryption_spacing.Text, this.top_pile_reinforcement_length.Text,
                                                this.pier_diameter.Text, this.pier_main_diameter.Text, this.pier_out_reinforcement_diameter.Text, 
                                                this.pier_protect_layer.Text, this.pile_link_type.Text, this.spiral_reinforcement_diameter.Text,
                                                this.pier_encryption_length.Text,this.pier_encryption_spacing.Text,this.pier_arc_length.Text);
                    }
                    if (MySqlHelper.ExecuteNonQuery(updatesql, CommandType.Text) > 0)
                    {
                        string sqlS = "select * from t_pile_info_reinforcement where id='" + _row["id"].ToString() + "'";
                        System.Data.DataTable dt = MySqlHelper.ExecuteDataTable(sqlS, CommandType.Text);
                        if (dt.Rows.Count > 0)
                        {
                            myrow = dt.Rows[0];
                        }
                        isConfirm = true;
                    }
                }
            }
        }

        private void pile_link_type_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(pile_link_type.SelectedIndex==0)
            {
                gxPier.Visible = false;
            }
            else
            {
                gxPier.Visible = true;
            }
        }
    }
}
